<?php
class job extends Controller
{
	function __constructor()
	{
		parent::__constructor();
	}

	function index()
	{
		is_logged_in();
		$this->load->view("job/index");
	}
	
	function showalljobs()
	{
		is_logged_in();
		
		$msg= "";
		$this->load->model("model_job");
		$allJobs = $this->model_job->getJobs(99999999);
		
		$data = array(	"crum" => build_crum( array("Jobs", "Show All Jobs") )
							  ,"jobs" => $allJobs
							  ,"msg" => $msg);
		$this->load->view("job/showalljobs",$data);
	}
	
	function newjob()
	{
		is_logged_in();
		
		$this->load->model("model_lu");
		$this->load->model("model_company");
		$this->load->model("model_job");
		$msg = "";
		
		$companies = $this->model_company->getAll();
		$job_pri = $this->model_lu->get("job_pri");
		$job_types = $this->model_lu->get("job_type");
		$data = array();
		
		
		if($this->input->post("action") == "newJob")
		{

			
			if($this->input->post("job_subject") != "")
			{

				$data = array(	
								"job_subject" => $this->input->post("job_subject")
								,"job_description" => $this->input->post("job_description")
								,"job_logged" => date("Y-m-d h:i:s")
								,"job_logged_by" => $_SESSION['user_id']
								,"job_start" => $this->input->post("job_start_date"). " " . $this->input->post("job_start_time_hr"). ":". $this->input->post("job_start_time_min"). ":00"
								,"job_end" => $this->input->post("job_end_date"). " " . $this->input->post("job_end_time_hr").":".$this->input->post("job_end_time_min"). ":00"
								,"job_type" => $this->input->post("job_type")
								,"job_priority" => $this->input->post("job_pri")
							);
							
				if($this->input->post("cod_client_name") != "")
				{
					$company = array(
						"company_name" => $this->input->post("cod_client_name")
						,"company_parent" => $_SESSION['user_company']
					);
					$this->model_company->newCompany($company);
					
					$company_id = $this->db->insert_id();
					$data['company_id'] = $company_id;
					
				}
				else
				{
		
					$data['company_id'] = $this->input->post("company_id");	
				}
				
				
				$this->load->model("model_job");
				$this->model_job->newJob($data);
				$msg = $this->input->post("job_subject") . " : Sucessfully added!";
			}
			else{
				$msg = "* Job subject required!";
			}
		}
		
	$data = array(	"crum" => build_crum( array("Jobs", "New Job", $this->model_job->getNextAutoIncrement()) )
					  ,"companies" => $companies
					  ,"job_pri" => $job_pri
					  ,"job_types" => $job_types
					  ,"msg" => $msg
						);
					  
	$this->load->view("job/newjob", $data);
					  
	}
	
	function editjob()
	{
		is_logged_in();
		
		$jobId = $this->uri->segment(3);
		$msg = "";
		
		$this->load->model("model_job");
		$this->load->model("model_lu");
		$this->load->model("model_company");
		$this->load->model("model_user");
		
		$companies = $this->model_company->getAll();
		$job_pri = $this->model_lu->get("job_pri");
		$job_types = $this->model_lu->get("job_type");
		$job_users = $this->model_user->getUsers();
		
		$job = $this->model_job->getJobs('', array("id","asc") ,array("job.id" => $jobId));	
	
		$data = array(	"crum" => build_crum( array("Jobs", "Edit Job", "#".$job[0]['id']. " " . word_limiter($job[0]['job_subject'], 6)) )
						,"job" => $job
						,"companies" => $companies
						,"job_pri" => $job_pri
						,"job_types" => $job_types
						,"job_users" => $job_users
						,"msg" => "Job #".$job[0]['id']
		);

		$this->load->view("job/editjob", $data);
	}
	
	function updatejob()
	{
		is_logged_in();
		
		if($this->input->post("action") == "update")
		{
			$jobId = $this->input->post('id');
			$this->load->model("model_job");
			
			$job_data = array(	"company_id" => $this->input->post("company_id")
							,"job_subject" => $this->input->post("job_subject")
							,"job_description" => $this->input->post("job_description")
							,"job_logged" => date("Y-m-d h:i:s")
							,"job_logged_by" => $this->input->post("job_logged_by")
							,"job_start" => $this->input->post("job_start_date"). " " . $this->input->post("job_start_time"). ":00"
							,"job_end" => $this->input->post("job_end_date"). " " . $this->input->post("job_end_time"). ":00"
							,"job_type" => $this->input->post("job_type")
							,"job_priority" => $this->input->post("job_pri")
						);
			$this->model_job->updateJob($job_data, $jobId);
			
			redirect("job/showalljobs/");		
		}
		
	}
	
	function newnote()
	{
		if($this->input->post("note_body") == "")
		{
			$this->load->view("job/notes");
		}
		else{
		
			$note_body = $this->input->post("note_body");
			$note_job_id = $this->input->post("note_job_id");
			$data = array("note_body" => $note_body);
			echo ("file :" .$this->input->post("userfile"));
			
			//die();
			
				
				$config = array();
				$config['upload_path'] = 'D:/wamp/www/ant/assets/uploads/notes/'.$note_job_id."/";
				$config['allowed_types'] = 'gif|jpg|png';
				$config['remove_spaces'] = true;
				
				echo "=============================";
				echo $config['upload_path'];
				echo "=============================";
				$this->load->library('upload', $config);
				
				if($this->upload->do_upload())
				{
					$udata = $this->upload->data();
					$data['note_img'] = $udata['full_path'];
				}
				else{
					var_dump($this->upload->display_errors());
					die();
				}
				//var_dump($data);
		}
	}
   
	



}